/**
 * Filename: PreviewAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/19 14:54
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.SnackBarUtils;
import com.github.postsanf.yinian.view.TouchImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;

public class PreviewAdapter extends PagerAdapter {
    private Activity context;
    private List<String> picUrls;
    private ArrayList<View> picViews;
    private Resources mResource;

    public void setResource(Resources mResource) {
        this.mResource = mResource;
    }

    public PreviewAdapter(Activity context, List<String> picUrls) {
        this.context = context;
        this.picUrls = picUrls;
        initImgs();
    }

    private void initImgs() {
        picViews = new ArrayList<View>();
        for (int i = 0; i < picUrls.size(); i++) {
            View view = View.inflate(context, R.layout.yn_item_image_preview, null);
            picViews.add(view);
        }
    }

    @Override
    public int getCount() {
        return picUrls.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public View instantiateItem(ViewGroup container, final int position) {
        View view = picViews.get(position);
        final PhotoView iv_image_preview = (PhotoView) view.findViewById(R.id.iv_image_preview);
        CheckBox preview_check = (CheckBox) view.findViewById(R.id.preview_check);
        if (mResource != null) {
            preview_check.setButtonDrawable(mResource.getDrawable(mResource.getIdentifier("check_sel", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
        }
        preview_check.setClickable(false);
//		preview_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//			@Override
//			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//				// 去除选中修改同步
//				if (!isChecked){
//					SelectedImg.mSelectedImage.remove(position);
//					((PreviewActivity)context).updateSize();
//				}else{
//					SelectedImg.mSelectedImage.add(position, picUrls.get(position));
//					((PreviewActivity)context).updateSize();
//				}
//			}
//		});
        String url = picUrls.get(position);
        if (!url.contains("http")) {
            url = "file://" + url;
        }
        ImageLoader.getInstance().displayImage(url, iv_image_preview,
                ImageOptHelper.getImgOptions(), new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                        String message = null;
                        switch (failReason.getType()) {     // 获取图片失败类型
                            case IO_ERROR:              // 文件I/O错误
                                message = "文件错误";
                                break;
                            case DECODING_ERROR:        // 解码错误
                                message = "解码错误";
                                break;
                            case NETWORK_DENIED:        // 网络延迟
                                message = "网络延迟";
                                break;
                            case OUT_OF_MEMORY:         // 内存不足
                                message = "内存不足";
                                break;
                            case UNKNOWN:               // 原因不明
                                message = "未知错误";
                                break;
                        }
//						ToastUtils.showToast(context, message, Toast.LENGTH_SHORT);
                        SnackBarUtils.makeShort(view, message).show();
                    }

                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        float scale = (float) loadedImage.getHeight() / loadedImage.getWidth();
                        int screenWidthPixels = DisplayUtils.getScreenWidthPixels(context);
                        int screenHeightPixels = DisplayUtils.getScreenHeightPixels(context);
                        int height = (int) (screenWidthPixels * scale);
                        if (height < screenHeightPixels) {
                            height = screenHeightPixels;
                        }
                        ViewGroup.LayoutParams params = iv_image_preview.getLayoutParams();
                        params.height = height;
                        params.width = screenWidthPixels;
                        iv_image_preview.setImageBitmap(loadedImage);
                    }
                });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public String getPic(int position) {
        return picUrls.get(position % picUrls.size());
    }
}